function search(arr, target) {
  const len = arr.length;
  let res = null;

  function run(start, end, t) {
    const mid = Math.floor((start + end) / 2);
    console.log(start, end, mid, t);
    if (t === arr[mid]) {
      res = mid;
      return true;
    }
    if (t === arr[start]) {
      res = start;
      return true;
    }
    if (t === arr[end]) {
      res = end;
      return true;
    }
    if (t > arr[mid]) {
      run(mid + 1, end, t);
      return false;
    }
    if (t < arr[mid]) {
      run(start, mid - 1, t);
      return false;
    } 
  }
  console.log(run(0, len, target));
  return res;
}

const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const res = search(arr, 2);
console.log('res', res);
